package com.ezops.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ezops.dao.facade.RuleManagementFacade;
import com.ezops.dto.ValidationRule;
import com.ezops.exception.EzopsUIException;

/**
 * Controller handling all rule related requests.s
 * 
 * @author vgaurav
 * 
 */
@Controller
@RequestMapping("/rule")
public class RuleController {

	@Autowired
	RuleManagementFacade facade;

	@RequestMapping(value = "/getValidationRules", method = RequestMethod.GET)
	public @ResponseBody
	List<ValidationRule> getValidationRules() throws EzopsUIException {
		List<ValidationRule> validationRuleList = null;
		try{
			validationRuleList = facade.getAllValidationRules();
		}
		catch(Exception e){
			throw new EzopsUIException("Rule file not found");
		}
		return validationRuleList;
	}

	@RequestMapping(value = "/saveValidationRules", method = RequestMethod.POST)
	public @ResponseBody boolean saveValidationRules(@RequestBody List<ValidationRule> updatedRules) {
		 facade.saveValidationRules(updatedRules);
		 return true;
	}
	
}
